package com.tomorrow.runner.widget;

import android.app.Activity;
import android.content.res.Resources;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TableRow;
import android.widget.TextView;

import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.ConfirmPassword;
import com.mobsandgeeks.saripaar.annotation.NumberRule;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Regex;
import com.mobsandgeeks.saripaar.annotation.Required;
import com.mobsandgeeks.saripaar.annotation.TextRule;
import com.tomorrow.runner.R;
import com.tomorrow.runner.util.Define;

import java.util.Calendar;

/**
 *
 * Created by tomorrow on 16-4-28.
 */
public class UserForm {

    public interface UserFormVisibleChangedListener {
        void onPanelVisibleChanged(int mode,boolean visible);
    }

    public static final int MODEL_LOGIN = 1;
    public static final int MODEL_REGISTER = 2;
    public static final int MODEL_EDIT = 3;

    private TableRow mRepeatPwdRow;
    private TableRow mNickNameRow;
    private TableRow mGanderRow;
    private TableRow mTallRow;
    private TableRow mBirthDayRow;
    private TableRow mWeightRow;


    public TextView mBirthday;

    @Required(order = 1,message = "不能为空")
    @TextRule(order = 2 ,maxLength = 12,minLength = 3, message = "长度为3-12个字符")
    public EditText mEditUsername;

    @Required(order = 3,message = "不能为空")
    @Password(order = 4)
    @TextRule(order = 10 ,maxLength = 12,minLength = 6, message = "长度为6-12个字符")
    @Regex(order = 11,pattern = Define.PASSWORD_PATTERN,message = "密码至少包含数字,字母,符号中的2种")
    public EditText mEditPwd;

    @ConfirmPassword(order = 5,message = "两次密码不一致")
    public EditText mEditRePwd;

    @Required(order = 6)
    @TextRule(order = 7 ,maxLength = 12,minLength = 3, message = "长度为3-12个字符")
    public EditText mEditNickname;

    public RadioButton mRadioMale;

    public RadioButton mRadioFeMale;

    @Required(order = 7)
    @NumberRule(order = 8,gt = 30,lt = 100,type = NumberRule.NumberType.DOUBLE,message = "该体重不太适合剧烈运动，请确认输入")
    public EditText mEditWeight;
    @Required(order = 9)
    @NumberRule(order = 8,gt = 60,lt = 250,type = NumberRule.NumberType.DOUBLE,message = "该身高不太适合剧烈运动，请确认输入")
    public EditText mEditTall;

    private int mModel = MODEL_LOGIN;

    private Activity mContext;

    private UserFormVisibleChangedListener mListener;
    private Validator mValidator;

    public void setFormVisibleListener(UserFormVisibleChangedListener listener) {
        this.mListener = listener;
    }
    public UserForm(Activity activity, int model){
        mContext = activity;
        mModel = model;
        mValidator = new Validator(this);
        initView();
    }

    public void setValidationListener(Validator.ValidationListener listener){
        mValidator.setValidationListener(listener);
    }
    public void validate() {
        mValidator.validate();
    }
    private void initView(){
        View form = mContext.findViewById(R.id.info_edit_panel);
        if (form == null){
            throw new Resources.NotFoundException("this activity did not include R.layout.user_info_form");
        }
        mRepeatPwdRow = (TableRow) form.findViewById(R.id.repeat_password_panel);
        mNickNameRow = (TableRow) form.findViewById(R.id.nickname_panel);
        mGanderRow = (TableRow) form.findViewById(R.id.gander_panel);
        mTallRow = (TableRow) form.findViewById(R.id.tall_panel);
        mBirthDayRow = (TableRow) form.findViewById(R.id.birthday_panel);
        mWeightRow = (TableRow) form.findViewById(R.id.weight_panel);

        mBirthday = (TextView) form.findViewById(R.id.birthday_label);
        mEditUsername = (EditText) form.findViewById(R.id.edit_username);
        mEditPwd = (EditText) form.findViewById(R.id.edit_password);
        mEditRePwd = (EditText) form.findViewById(R.id.edit_repeat_password);
        mEditNickname = (EditText) form.findViewById(R.id.edit_nickname);
        mEditWeight = (EditText) form.findViewById(R.id.edit_weight);
        mEditTall = (EditText) form.findViewById(R.id.edit_tall);
        mRadioMale = (RadioButton) form.findViewById(R.id.edit_gander_male);
        mRadioFeMale = (RadioButton) form.findViewById(R.id.edit_gander_female);
        if (mContext instanceof View.OnClickListener){
            mBirthday.setOnClickListener((View.OnClickListener) mContext);
        }
        refreshView();
    }

    private void refreshView(){
        switch (mModel){
            case MODEL_LOGIN:
                showRegisterPanel(false);
                break;
            case MODEL_REGISTER:
                showRegisterPanel(true);
                break;
            case MODEL_EDIT:
                mEditUsername.setEnabled(false);
                showRegisterPanel(true);
                break;
        }
    }
    public int getModel() {
        return mModel;
    }
    public void setModel(int model){
        mModel = model;
        refreshView();
    }

    private void showRegisterPanel(boolean show){
        //clear data
        mEditPwd.setText("");
        mEditRePwd.setText("");
        mEditNickname.setText("");
        mEditWeight.setText("");
        mEditTall.setText("");
        int y = Calendar.getInstance().get(Calendar.YEAR);
        int m = Calendar.getInstance().get(Calendar.MONTH);
        int d = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        mBirthday.setText(mContext.getString(R.string.default_date,y,m,d));

        //show or hide
        if (show){
            mRepeatPwdRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_in));
            mNickNameRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_in));
            mGanderRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_in));
            mTallRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_in));
            mBirthDayRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_in));
            mWeightRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_in));
        }
        else{
            mRepeatPwdRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_out));
            mNickNameRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_out));
            mGanderRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_out));
            mTallRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_out));
            mBirthDayRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_out));
            mWeightRow.setAnimation(AnimationUtils.loadAnimation(mContext,android.R.anim.fade_out));
        }

        mRepeatPwdRow.setVisibility(show ? View.VISIBLE:View.GONE);
        mNickNameRow.setVisibility(show ? View.VISIBLE:View.GONE);
        mGanderRow.setVisibility(show ? View.VISIBLE:View.GONE);
        mTallRow.setVisibility(show ? View.VISIBLE:View.GONE);
        mBirthDayRow.setVisibility(show ? View.VISIBLE:View.GONE);
        mWeightRow.setVisibility(show ? View.VISIBLE:View.GONE);
        if (mListener != null){
            mListener.onPanelVisibleChanged(mModel,show);
        }
    }
}
